package com.guigu.pd.base.controller;

import java.util.ArrayList;
import java.util.List;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.common.CustomIdGenerator;
import com.guigu.pd.base.entity.GoodsTypeEntity;
import com.guigu.pd.base.entity.TruckTypeGoodsTypeEntity;
import com.guigu.pd.base.service.GoodsTypeService;
import com.guigu.pd.base.service.TruckTypeGoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.base.entity.TruckTypeEntity;
import com.guigu.pd.base.service.TruckTypeService;
import com.guigu.common.utils.R;



/**
 * 车辆类型表
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:09:01
 */
@RestController
@RequestMapping("base/trucktype")
public class TruckTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private TruckTypeGoodsTypeService truckTypeGoodsTypeService;

    @Autowired
    private TruckTypeService truckTypeService;

    @Autowired
    private CustomIdGenerator customIdGenerator;



    /**
     * 新增车辆类型，同时需要关联类型
     * @param
     * @return 车辆类型信息
     */
    @PostMapping("/saveTruckType")
    public R saveTruckType(@RequestBody TruckTypeEntity truckType){

        //设置货物类型id
        truckType.setId(customIdGenerator.nextId(truckType)+"");

        //添加货物信息
        boolean save = truckTypeService.save(truckType);

        TruckTypeGoodsTypeEntity truckTypeGoodsTypeEntity = new TruckTypeGoodsTypeEntity();

        boolean save1 = false;

        //添加关联商品类型数据
        for (GoodsTypeEntity goodsTypeEntity : truckType.getGoodsTypeEntities()) {
            truckTypeGoodsTypeEntity.setId(customIdGenerator.nextId(truckTypeGoodsTypeEntity)+"");
            truckTypeGoodsTypeEntity.setTruckTypeId(truckType.getId());
            truckTypeGoodsTypeEntity.setGoodsTypeId(goodsTypeEntity.getId());

            save1 = truckTypeGoodsTypeService.save(truckTypeGoodsTypeEntity);

        }

        if(save1){
            return R.ok("添加成功");
        }

        return R.ok("添加失败");
    }


    /**
     * 删除车辆类型--逻辑删除
     *
     * @param id 车辆类型id
     * @return 返回信息
     */
    @DeleteMapping("/delecttrucktypebyid/{id}")
    public R delecttrucktypebyid(@PathVariable(name = "id") String id) {

        boolean b = truckTypeService.removeById(id);
        if(b){
            return R.ok("删除成功");
        }
        return R.ok("删除失败");

    }

    /**
     * 根据id查询车辆类型
     * @param id
     * @return
     */
    @GetMapping("/findtrucktypeById/{id}")
    public TruckTypeEntity findtrucktypeById(@PathVariable(name = "id") String id){


         TruckTypeEntity truckType = truckTypeService.getById(id);


        //还需要查询当前货物类型关联的车辆类型的id查出车辆类型信息

        List<GoodsTypeEntity> gtypelist = new ArrayList<GoodsTypeEntity>();

        QueryWrapper<TruckTypeGoodsTypeEntity> queryWrapper = new QueryWrapper<TruckTypeGoodsTypeEntity>();

        queryWrapper.eq("truck_type_id",id);

        List<TruckTypeGoodsTypeEntity> list = truckTypeGoodsTypeService.list(queryWrapper);

        for (TruckTypeGoodsTypeEntity truckTypeGoodsTypeEntity : list) {
            String truckTypeId = truckTypeGoodsTypeEntity.getGoodsTypeId();
            //根据商品类型id查询数据

            GoodsTypeEntity byId = goodsTypeService.getById(truckTypeId);

            gtypelist.add(byId);
        }


        truckType.setGoodsTypeEntities(gtypelist);

        return truckType;
    }

    /**
     * 更新货物类型信息
     * @param truckType
     * @return
     */
    @PutMapping("/updatetrucktypeById")
    public R updatetrucktypeById(@RequestBody TruckTypeEntity truckType){


        boolean save = false;

        //更新货物类型的普通属性
        boolean b = truckTypeService.updateById(truckType);

        //更新关联信息
        //获取到前端传过来的车辆类型数据
        List<GoodsTypeEntity> goodsTypeIds = truckType.getGoodsTypeEntities();


        //判断是否有数据
        //有数据就删除重加
        if(goodsTypeIds != null && goodsTypeIds.size() > 0){

            QueryWrapper<TruckTypeGoodsTypeEntity> queryWrapper = new QueryWrapper<TruckTypeGoodsTypeEntity>();

            queryWrapper.eq("truck_type_id",truckType.getId());

            //先删除车辆类型和货物类型数据
            truckTypeGoodsTypeService.remove(queryWrapper);


            TruckTypeGoodsTypeEntity truckTypeGoodsType = new TruckTypeGoodsTypeEntity();
            //重新添加数据
            for (GoodsTypeEntity goodsType : goodsTypeIds) {

                truckTypeGoodsType.setId(customIdGenerator.nextId(truckTypeGoodsType)+"");
                truckTypeGoodsType.setTruckTypeId(goodsType.getId());
                truckTypeGoodsType.setGoodsTypeId(truckType.getId());

                truckTypeGoodsTypeService.save(truckTypeGoodsType);

            }

            return R.ok("修改成功");
        }

        if(b){
            return R.ok("修改成功");
        }

        return R.ok("修改失败");


    }


    /**
     * 分页查询车辆类型
     * @param pageNum
     * @param pagesize
     * @param TruckTypename
     * @return
     */
    @PostMapping("/querytrucktypefy/{pageNum}/{pagesize}")
    public PageVo<TruckTypeEntity> querytrucktypefy(@PathVariable Integer pageNum,
                                                    @PathVariable Integer pagesize,
                                                    @RequestBody TruckTypeEntity TruckType
                                                    ){
        PageVo<TruckTypeEntity> list = truckTypeService.querytrucktypefy(TruckType,pageNum, pagesize);
        return list;
    }


    /**
     * 获取车型类型列表
     */
    @GetMapping("/findtrucktypeAll")
    public List<TruckTypeEntity> findtrucktypeAll() {

        return truckTypeService.list();
    }

}
